--[[
    quick定时器
    local sharedScheduler = CCDirector:sharedDirector():getScheduler()
    sharedScheduler:scheduleScriptFunc(listener, interval, false)
    参数listener是需要定时执行的函数，interval这里是0（每帧执行listener）
    第三个参数false，表示不暂停schedule，如果为true，schedule没有启动
]]

local scheduler = require("framework.scheduler");
local SchedulerMgr = class("SchedulerMgr");

function SchedulerMgr:ctor()
    self._intervalCbk = {};
    self:scheduleGlobal(handler(self,self.Interval),1);

    self._frameCbk = {};
    self:scheduleGlobal(handler(self,self.Frame),1/10);
end

function SchedulerMgr:Interval(dt)
    for key, var in ipairs(self._intervalCbk) do
        var(dt); 
    end
end

--添加 每隔1s 回调函数
function SchedulerMgr:addIntervalCbk(cbk)
    table.insert(self._intervalCbk,cbk);
end

function SchedulerMgr:delIntervalCbk(cbk)
    for key, var in pairs(self._intervalCbk) do
        if var == cbk then
            table.remove(self._intervalCbk,key);
            mgrs.log:printNotice("delIntervalCbk");
            break;
        end
    end
end


function SchedulerMgr:Frame(dt)
    for key, var in ipairs(self._frameCbk) do
        var(dt); 
    end
end
--添加 每帧 回调函数
function SchedulerMgr:addFrameCbk(cbk)
    table.insert(self._frameCbk,cbk);
end

function SchedulerMgr:delFrameCbk(cbk)
    for key, var in pairs(self._frameCbk) do
        if var == cbk then
            table.remove(self._frameCbk,key);
            mgrs.log:printNotice("delFrameCbk");
            break;
        end
    end
end

function SchedulerMgr:delCbk(cbks)
    for k, v in pairs(cbks) do
        self:delIntervalCbk(v);
        self:delFrameCbk(v);
    end
end


--每帧执行listener
function SchedulerMgr:scheduleUpdateGlobal(listener)
    return scheduler.scheduleUpdateGlobal(listener);
end
--interval间隔执行listener
function SchedulerMgr:scheduleGlobal(listener, interval)
    return scheduler.scheduleGlobal(listener,interval);
end
--取消schedule
function SchedulerMgr:unscheduleGlobal(handle)
    scheduler.unscheduleGlobal(handle);
end
--延迟time调用一次listener
function SchedulerMgr:performWithDelayGlobal(listener, time)
    return scheduler.performWithDelayGlobal(listener,time);
end


mgrs.scheduler = nil;
mgrs.singleClass(mgrs,SchedulerMgr,"scheduler");

--endregion
